/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.chl.multi;

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 陈宏亮
 */
public class ThreadAccessMethod {

    private static final int SIZE = 2000;

    public static void main(String[] args) {
        moreObject();
        oneObject();
    }

    public static void oneObject() {
        long b = System.currentTimeMillis();
        final MethodClass mc = new MethodClass();
        final CountDownLatch cd = new CountDownLatch(SIZE);
        for (int i = 0; i < SIZE; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mc.doSome();
                    cd.countDown();
                }
            }).start();
        }
        try {
            cd.await();
            long e = System.currentTimeMillis();
            System.out.println("oneObject-time:" + (e - b));
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadAccessMethod.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void moreObject() {
        long b = System.currentTimeMillis();
        final CountDownLatch cd = new CountDownLatch(SIZE);
        for (int i = 0; i < SIZE; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new MethodClass().doSome();
                    cd.countDown();
                }
            }).start();
        }
        try {
            cd.await();
            long e = System.currentTimeMillis();
            System.out.println("moreObject-time:" + (e - b));
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadAccessMethod.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class MethodClass {

    public void doSome() {
        try {
            Thread.sleep(300);
        } catch (InterruptedException ex) {
            Logger.getLogger(MethodClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
